package com.tingyuge.thinking.spring.dependency.lookup;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/6/27
 */
public class NoUniqueBeanDefinitionExceptionDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //注册 configuration class（配置类）
        applicationContext.register(NoUniqueBeanDefinitionExceptionDemo.class);
        //启动应用上下文
        applicationContext.refresh();

        try {
            String bean = applicationContext.getBean(String.class);
        } catch (BeansException e) {
            e.printStackTrace();
            if(e instanceof NoUniqueBeanDefinitionException){
                NoUniqueBeanDefinitionException uniqueBeanDefinitionException = (NoUniqueBeanDefinitionException)e;
                System.err.println(uniqueBeanDefinitionException.getNumberOfBeansFound());
            }
        }
    }


    @Bean
    public String bean1(){
        return "hello world";
    }
    @Bean
    public String bean2(){
        return "hello world";
    }
}
